//
//  ChangePwdViewController.m
//  Bees
//
//  Created by NowYouCanSee on 2019/1/4.
//  Copyright © 2019 xiaohan. All rights reserved.
//

#import "ChangePwdViewController.h"
#import "UserServer.h"
@interface ChangePwdViewController ()<UITextFieldDelegate>
@property (nonatomic,strong) UITextField *oldTF;
@property (nonatomic,strong) UITextField *psw1;
@property (nonatomic,strong) UITextField *psw2;
@property (nonatomic,strong) UITextField *pswTipTF;
@property (nonatomic,strong) UserServer *userServer;
@property (nonatomic,assign) BOOL changeBeesPwd;

@end

@implementation ChangePwdViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.changeBeesPwd = IsNilOrNull(self.wallet);
    [self setupUI];
}

- (void)setupUI{
    self.title = self.changeBeesPwd?XLocalized(@"修改BEES密码"):XLocalized(@"修改钱包密码");
    
    UILabel *tipLab = [UILabel new];
    tipLab.font = kBoldFont(16);
    tipLab.textColor = UIColorHex(333333);
    tipLab.text = XLocalized(@"当前密码");
    [self.contentView addSubview:tipLab];
    [tipLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.mas_equalTo(kNormalSpace);
    }];
    self.oldTF = [UITextField new];
    self.oldTF.secureTextEntry = YES;
    self.oldTF.delegate = self;
    self.oldTF.layer.cornerRadius = AdaptedWidth(10);
    self.oldTF.font = kDefaultFont(14);
    self.oldTF.backgroundColor = UIColorHex(#F7F7F7);
    self.oldTF.placeholder = XLocalized(@"请输入当前密码");
    UIView*letfV2 = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 8, AdaptedWidth(30))];
    self.oldTF.leftView = letfV2;
    self.oldTF.leftViewMode = UITextFieldViewModeAlways;
    [self.contentView addSubview:self.oldTF];
    [self.oldTF mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(kNormalSpace);
        make.right.mas_equalTo(-kNormalSpace);
        make.height.mas_equalTo(AdaptedWidth(37));
        make.top.equalTo(tipLab.mas_bottom).offset(AdaptedHeight(18));
    }];
    UILabel *tipLab2 = [UILabel new];
    tipLab2.font = kBoldFont(16);
    tipLab2.textColor = UIColorHex(333333);
    tipLab2.text = XLocalized(@"新密码");
    [self.contentView addSubview:tipLab2];
    [tipLab2 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.oldTF.mas_bottom).offset(AdaptedHeight(25));
        make.left.mas_equalTo(kNormalSpace);
    }];
    self.psw1 = [UITextField new];
    self.psw1.secureTextEntry = YES;
    self.psw1.delegate = self;
    self.psw1.borderStyle = UITextBorderStyleNone;
    self.psw1.layer.cornerRadius = AdaptedWidth(10);
    self.psw1.backgroundColor = UIColorHex(#F7F7F7);
    self.psw1.placeholder = XLocalized(@"请输入新密码");;
    self.psw1.font = kDefaultFont(14);
    UIView*letfV = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 8, AdaptedWidth(30))];
    self.psw1.leftView = letfV;
    self.psw1.leftViewMode = UITextFieldViewModeAlways;
    [self.contentView addSubview:self.psw1];
    [self.psw1 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(tipLab2.mas_bottom).offset(AdaptedWidth(15));
        make.left.mas_equalTo(kNormalSpace);
        make.right.mas_equalTo(-kNormalSpace);
        make.height.mas_equalTo(AdaptedWidth(37));
    }];
    self.psw2 = [UITextField new];
    self.psw2.delegate = self;
    self.psw2.secureTextEntry = YES;
    self.psw2.borderStyle = UITextBorderStyleNone;
    self.psw2.layer.cornerRadius = AdaptedWidth(10);
    self.psw2.backgroundColor = UIColorHex(#F7F7F7);
    self.psw2.placeholder = XLocalized(@"请再次输入新密码");;
    self.psw2.font = kDefaultFont(14);
    UIView*letfV1 = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 8, AdaptedWidth(30))];
    self.psw2.leftView = letfV1;
    self.psw2.leftViewMode = UITextFieldViewModeAlways;
    [self.contentView addSubview:self.psw2];
    [self.psw2 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.psw1.mas_bottom).offset(AdaptedWidth(15));
        make.left.mas_equalTo(kNormalSpace);
        make.right.mas_equalTo(-kNormalSpace);
        make.height.mas_equalTo(AdaptedWidth(37));
    }];
    
    if (self.changeBeesPwd) {
        UILabel*tipLab1 = [UILabel new];
        tipLab1.numberOfLines = 0;
        tipLab1.font = kDefaultFont(10);
        tipLab1.textColor = UIColorHex(999999);
        tipLab1.text =XLocalized(@"密码长度8-16位，须包含数字、字母、符号至少2种或以上元素。");
        [self.contentView addSubview:tipLab1];
        [tipLab1 mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(kNormalSpace);
            make.right.mas_equalTo(-kNormalSpace);
            make.top.equalTo(self.psw2.mas_bottom).offset(AdaptedHeight(10));
        }];
    }else{
        UILabel *tipLab3 = [UILabel new];
        tipLab3.font = kBoldFont(16);
        tipLab3.textColor = UIColorHex(333333);
        tipLab3.text = XLocalized(@"设置密码提示（非必填项）");
        [self.contentView addSubview:tipLab3];
        [tipLab3 mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.psw2.mas_bottom).offset(AdaptedHeight(25));
            make.left.mas_equalTo(kNormalSpace);
        }];
        self.pswTipTF = [UITextField new];
        self.pswTipTF.delegate = self;
        self.pswTipTF.borderStyle = UITextBorderStyleNone;
        self.pswTipTF.layer.cornerRadius = AdaptedWidth(10);
        self.pswTipTF.backgroundColor = UIColorHex(#F7F7F7);
        self.pswTipTF.placeholder = XLocalized(@"请输入您的密码提示");;
        self.pswTipTF.font = kDefaultFont(14);
        UIView*letfV1 = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 8, AdaptedWidth(30))];
        self.pswTipTF.leftView = letfV1;
        self.pswTipTF.leftViewMode = UITextFieldViewModeAlways;
        [self.contentView addSubview:self.pswTipTF];
        [self.pswTipTF mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(tipLab3.mas_bottom).offset(AdaptedWidth(15));
            make.left.mas_equalTo(kNormalSpace);
            make.right.mas_equalTo(-kNormalSpace);
            make.height.mas_equalTo(AdaptedWidth(37));
        }];
    }
  
    XThemeFlatButton*importBtn = [XThemeFlatButton button];
    [importBtn setTitle:XLocalized(@"完成") forState:UIControlStateNormal];
    [self.contentView addSubview:importBtn];
    [importBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        if (self.changeBeesPwd) {
            make.top.equalTo(self.psw2.mas_bottom).offset(AdaptedHeight(67));
        }else{
            make.top.equalTo(self.pswTipTF.mas_bottom).offset(AdaptedHeight(67));
        }
        make.left.mas_equalTo(kNormalSpace);
        make.right.mas_equalTo(-kNormalSpace);
        make.height.mas_equalTo(AdaptedWidth(40));
        make.bottom.mas_equalTo(0);
    }];
    
    kWeakSelf(self);
    [importBtn addTapBlock:^(UIButton * _Nonnull btn) {
        [weakself.view endEditing:YES];
        if (!weakself.oldTF.text.length) {
            [weakself.view makeToast:weakself.oldTF.placeholder];
            return;
        }
        if (!weakself.psw1.text.length) {
            [weakself.view makeToast:weakself.psw1.placeholder];
            return;
        }
        if (!weakself.psw2.text.length) {
            [weakself.view makeToast:weakself.psw2.placeholder];
            return;
        }
        if (self.changeBeesPwd) {
            if (![RegularExp isValidatePassword:weakself.psw1.text] || weakself.psw1.text.length<8) {
                [weakself.view makeToast:XLocalized(@"请设置有效密码!")];
                return;
            }
        }else{
            if (![RegularExp isValidateABC123:weakself.psw1.text] || weakself.psw1.text.length<8) {
                [weakself.view makeToast:XLocalized(@"请设置有效密码!")];
                return;
            }
        }
        
        if (![weakself.psw1.text isEqualToString:weakself.psw2.text]) {
            [weakself.view makeToast:XLocalized(@"密码不一致!")];
            return;
        }
        if (self.changeBeesPwd) {
            weakself.userServer.old_password = weakself.oldTF.text;
            weakself.userServer.password = weakself.psw1.text;
            weakself.userServer.re_password = weakself.psw2.text;
            [MBProgressHUD showLoadTitile:@"" ToView:weakself.view];
            [weakself.userServer changePasswordAccountComplete:^(BOOL success, NSString * _Nonnull msg) {
                [MBProgressHUD hideHUDForView:weakself.view];
                [kAppWindow makeToast:msg];
                if (success) {
                    [weakself backBtnClicked];
                }
            }];
        }else{
            if ([[weakself.oldTF.text sha256String] isEqualToString:weakself.wallet.password_sha]) {
                weakself.wallet.password_sha = [weakself.psw1.text sha256String];
                weakself.wallet.wallet_private_AES_key = [WalletTool encryptWalletPrivatekey:[WalletTool decryptWallet:weakself.wallet password:weakself.oldTF.text] password:weakself.psw1.text];
                weakself.wallet.password_tip = self.pswTipTF.text.length?self.pswTipTF.text:@"";
                [[WalletTool sharedWalletTool].walletUseFingerprint removeObjectForKey:weakself.wallet.wallet_uid];
                if ([[XHDataBaseManager shareInstance]updateWalletToDB:weakself.wallet]) {
                    [kAppWindow makeToast:XLocalized(@"修改密码成功!")];
                    [weakself backBtnClicked];
                }
            }else{
                [weakself.view makeToast:XLocalized(@"旧密码错误!")];
                return;
            }
        }
      
    }];
}

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
    if (self.changeBeesPwd) {
        NSString *tem = [[string componentsSeparatedByCharactersInSet:[NSCharacterSet whitespaceCharacterSet]]componentsJoinedByString:@""];
        if (![string isEqualToString:tem])return NO;
        return YES;
    }else{
        if (textField == self.pswTipTF) {
            return YES;
        }
        return [RegularExp controllABC123WithString:textField.text inRange:range inputString:string];
    }
}

- (UserServer*)userServer{
    if (!_userServer) {
        _userServer = [UserServer new];
    }
    return _userServer;
}


@end
